var utils = require('util');
var svn = require('svnintegration');

exports.getChangeSets = function (req, res) {
    console.log("Request received");
    if (req.method == 'POST') {
        console.log("Method POST");
        var body = '';
        var svnClient = new svn();

        svnClient.userName = req.body.userName;
        svnClient.password = req.body.password;
        svnClient.path = req.body.path;
        var param = { "size": 10 };
        if (req.body && req.body.size) {
            param.size = req.body.size;
        }
        svnClient.getLog(param, function (err, data) {
            console.log("Writing Response");
            res.write(JSON.stringify(data));
            res.end();
        });
    }
    else {
        res.end();
    }
}

exports.getChangeSetDetails = function (req, res) {
    console.log("request received");
    if (req.method == 'POST') {

        var body = '';
        var svnClient = new svn();

        svnClient.userName = req.body.userName;
        svnClient.password = req.body.password;
        svnClient.path = req.body.path;
        var param = { "changeset": 0 };

        if (req.body && req.body.changeset) {
            param.changeset = req.body.changeset;
        }

        svnClient.getChangesetDetails(param, function (err, data) {
            console.log("Writing Response");
            console.log(utils.inspect(data, {depth: null}));
            if (data) {
                res.write(JSON.stringify(data));
            }
            else {
                console.log("Data not received.");
            }
            res.end();
        });
    }
    else {
        res.end();
    }
}